<?php
/**
 * Miffos framework
 * ----------------------------------------------
 *
 * Copyright (c) 2008, 2009 Martin Sadový (http://sodae.cz)
 *
 * This source file is subject to the "Miffos license" that is bundled
 * with this package in the file license.txt.
 *
 * @copyright  Copyright (c) 2008, 2009 Martin Sadový
 * @license    license.txt
 * @link       http://miffos.sodae.cz
 * @package    miffos
 * @version    $Id$
 */

require_once dirname(__FILE__).'/Library/Cache.php';

class Component
{
    /**
     * List class
     * @var array
     */
    public $classes = array();

    /**
     * Dir class
     * @var string
     */
    public $dir;



    public function __construct($dir)
    {
        $this->dir = $dir;
        $this->load();
    }



    /**
     * Load
     * @return array
     */
    public function load()
    {
        if(is_dir(Cache::$dir)){
            $cache = new Cache('Miffos\Component\Library');
            if(!$cache['list']){
                $cache['list'] = $this->scanDir();
            }else{
                $this->classes = $cache['list'];
            }
            return $cache['list'];
            
        }else{
            return $this->scanDir();
            
        }
        
    }



    /**
     * Scan dir
     * @param string $dir
     * @return list class from dir
     */
    public function scanDir($dir=null)
    {
        if(is_null($dir)) 
            $dir = $this->dir;

        foreach(glob($dir.'/*') as $name){
          $name = realpath($name);
          if( is_dir($name) AND !preg_match('#^\.(svn|cvs)$#i', $name) AND ($name != $dir)){
              $this->scanDir($name);
          }else{
              $this->analyzeFile($name);
          }
        }
        return $this->classes;
    }



    /**
     * Analysing file for class
     * @param string $path
     */
    private function analyzeFile($path)
    {
        $isObject = false;
        foreach (token_get_all(file_get_contents($path)) as $token) {
          if ($token[0] == T_CLASS OR $token[0] == T_INTERFACE) {
              $isObject = true;
          } elseif ($token[0] == T_STRING && $isObject) {
              $this->classes[$token[1]] = $path;
              $isObject = false;
          }
        }
    }



    /**
     * Register method as autoloader class
     */
    public function register()
    {
        spl_autoload_register(array($this, "loadClass"));
    }



    /**
     * Load class by name
     * @param string $name
     * @param bool $secondTry
     */
    public function loadClass($name,$secondTry=false)
    {
        if( array_key_exists($name, $this->classes) ){
          require_once $this->classes[$name];
        }elseif(!$secondTry){
          $this->load();
          $this->loadClass($name,true);
        }else{
          throw new Exception("Class '{$name}' not found !");
        }
    }
}
?>